package com.hsnn.medstgmini.base.dict.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.model.DicDrugmaterial;
import com.hsnn.medstgmini.base.dict.model.DicDrugmaterialLog;
import com.hsnn.medstgmini.base.dict.service.DicDrugmaterialLogManager;
import com.hsnn.medstgmini.base.dict.service.DicDrugmaterialManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * 基础库 数据字典 材质字典 控制层
 * 
 * @author hss@njhsnn 2015年6月8日
 */
@Controller
@RequestMapping("/dicDrugmaterialLog")
public class DicDrugmaterialLogController {
	
	private static final Logger log = Logger.getLogger(DicDrugmaterialLogController.class);

	@Source
	private DicDrugmaterialLogManager dicDrugmaterialLogManager;
	
	@Autowired
	private HttpServletRequest request;
	

	@Source
	private DicDrugmaterialManager dicDrugmaterialManager;

	@Autowired
	public void setDicDrugmaterialLogManager(
			DicDrugmaterialLogManager dicDrugmaterialLogManager) {
		this.dicDrugmaterialLogManager = dicDrugmaterialLogManager;
	}

	@Autowired
	public void setDicDrugmaterialManager(
			DicDrugmaterialManager dicDrugmaterialManager) {
		this.dicDrugmaterialManager = dicDrugmaterialManager;
	}

	/**
	 * 基础库 数据字典 材质字典 记录维护增加
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/dicDrugmaterialLogAdd", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String dicDrugmaterialLogAdd(HttpServletRequest request,
			HttpServletResponse response) {
		printParams();
		DicDrugmaterialLog dicDrugmaterialLog = new DicDrugmaterialLog();// 类
		String Id = request.getParameter("materialId");
		String[] strId = Id.split(",");
		Date date = new Date();
		SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = fomat.format(date);
		String materialId = "";
		String materialName = "";
		String materialCode = "";
		Integer status = (Integer) 0;
		String updUser = "";
		short sortId = (short) 0;
		String logId = "";
		int strlong = strId.length;
		for (int j = 0; j < strlong; j++) {
			List<DicDrugmaterial> list = dicDrugmaterialManager
					.queryDicDrugmaterialById(strId[j]);
			for (int i = 0; i < strlong;) {
				materialId = ((DicDrugmaterial) list.get(0)).getMaterialId();
				materialName = ((DicDrugmaterial) list.get(0)).getMaterialName();
				materialCode = ((DicDrugmaterial) list.get(0)).getMaterialCode();
				status = ((DicDrugmaterial) list.get(0)).getStatus();
				updUser = ((DicDrugmaterial) list.get(0)).getUpdUser();
				sortId = (short) ((DicDrugmaterial) list.get(0)).getSortId();
				dicDrugmaterialLog.setMaterialId(materialId);// 当前行到到id
				dicDrugmaterialLog.setMaterialName(materialName);// 材质名称
				dicDrugmaterialLog.setMaterialCode(materialCode);// 材质编码
				dicDrugmaterialLog.setUpdDatetime(Timestamp.valueOf(strDate));// 更新时间
				dicDrugmaterialLog.setUpdUser(updUser);// 更新人
				dicDrugmaterialLog.setStatus(status);// 状态
				dicDrugmaterialLog.setSortId(sortId);// 排序
				dicDrugmaterialLog.setLogId(logId);
				dicDrugmaterialLogManager.add(dicDrugmaterialLog);
				break;
			}
		}
		return "redirect:/dicDrugmaterial/dicDrugmaterialListJsp.html";
	}
	
	

	/**
	 * 基础库 数据字典 材质字典 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/log", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String dicDrugmaterialLogDetail(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		printParams();
		String materialId = request.getParameter("materialId");
		request.setAttribute("materialCode", request.getParameter("materialCode"));
		String materialName = new String(request.getParameter("materialName").getBytes("iso8859-1"),"utf-8");
		request.setAttribute("materialName", materialName);
		request.setAttribute("materialId", materialId);
		return "/base/dict/drugMaterialHis";// 跳转到详情页面
	}

	/**
	 * 基础库 数据字典 材质字典 查询及条件查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/dicDrugmaterialLogList")
	@ResponseBody
	public Pagination getDicDrugmaterialLogList(HttpServletRequest request) {
		printParams();
		Pagination page = new Pagination(request);
		try {
			String materialId = request.getParameter("materialId");
			String date1 = request.getParameter("date1");
			String date2 = request.getParameter("date2");
			if(StringUtils.isNotBlank(date1)){
				page.getConditions().put("date1", date1.trim());
			}
			if(StringUtils.isNotBlank(date2)){
				page.getConditions().put("date2", date2.trim());
			}
			if(StringUtils.isNotBlank(materialId)){
				page.getConditions().put("materialId", materialId);
			}
			dicDrugmaterialLogManager.queryDicDrugmaterialLogByName(page);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}

	
	/**
	 * 
	 *@category 材质字典维护记录详情页面
	 *@author 言科
	 *@date 2015年9月11日
	 *String
	 */
	@RequestMapping("/toDicDrugmaterialLogListDetail")
	public String  toDicDrugmaterialLogListDetail() {
		log.info("enter toDicDrugmaterialLogListDetail method!");
		printParams();
		try {
			Pagination  page = new Pagination();
			page.getConditions().put("logId", request.getParameter("logId"));
			List<Map<String,Object>> list = dicDrugmaterialLogManager.getDicDrugmaterialLogListDetail(page);
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
			String[] cColunm = {"材质编码","材质名称","状态"};
			String[] eColunm = {"materialCode","materialName","status"};
			for(int i = 0;i<cColunm.length;i++){
				Map<String,Object> map =  new HashMap<String,Object>();
				map.put("colunm", cColunm[i]);//字段名
				map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"停用");//修改后数据
				map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"停用");//修改前数据
				if(list.size()==1){
					map.put("red", "1");//是否变红加粗 1是 0 否
				}else{
					//判断修改后与修改前数据是否相等
					if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
						map.put("red", "1");
					}else{
						map.put("red", "0");
					}
				}
				mapList.add(map);
			}
			for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
				if("1".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			for(int i = 0;i<mapList.size();i++){
				if("0".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			request.setAttribute("dataList", dataList);
			request.setAttribute("list", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/base/dict/drugmaterialLogListDetail";
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}

}
